package com.fingerth.crawler.views.tabview

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.fingerth.crawler.R
import com.fingerth.crawler.databinding.ViewHomeTabViewBinding

class HomeTabView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, private val position: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {

    private val vBinding: ViewHomeTabViewBinding = ViewHomeTabViewBinding.inflate(LayoutInflater.from(context), this)
    private var isSelect: Boolean = false
    private val pIcon: Pair<Int, Int>

//    private val icons = listOf(R.drawable.img_place_holder, R.drawable.img_place_holder, R.drawable.img_place_holder)

    init {
        pIcon = when (position) {
            0 -> {
                vBinding.tv.text = "小说"
                Pair(R.drawable.ic_menu_book, R.drawable.ic_menu_book)
            }
            1 -> {
                vBinding.tv.text = "漫画"
                Pair(R.drawable.ic_menu_comic, R.drawable.ic_menu_comic)
            }
            else -> {
                vBinding.tv.text = "视频"
                Pair(R.drawable.ic_menu_video, R.drawable.ic_menu_video)
            }
        }
        setSelect(position == 0, true)
    }

    fun setSelect(select: Boolean = true, isInit: Boolean = false) {
        if (isInit) setV(select) else if (isSelect != select) setV(select)
    }

    private fun setV(select: Boolean) {
        isSelect = select
        if (select) {
            vBinding.iv.setColorFilter(Color.BLACK)
            vBinding.iv.setImageResource(pIcon.second)
            vBinding.tv.setTextColor(Color.BLACK)
            //iv.setImageResource(pIcon.second)
            //tv.setTextColor(ContextCompat.getColor(context, R.color.black_43))
        } else {
//            iv.setImageResource(pIcon.first)
//            tv.setTextColor(ContextCompat.getColor(context, R.color.white_aa))
            vBinding.iv.setImageResource(pIcon.first)
            vBinding.iv.setColorFilter(Color.rgb(0xaa, 0xaa, 0xaa))
            vBinding.tv.setTextColor(Color.rgb(0xaa, 0xaa, 0xaa))
        }
    }


}